package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.mysql.MysqlLink;
import com.ruoyi.system.domain.DabaTable;
import com.ruoyi.system.domain.DatabaseDetails;
import com.ruoyi.system.service.IDabaTableService;
import com.ruoyi.system.service.IDatabaseDetailsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DatabaseTable;
import com.ruoyi.system.service.IDatabaseTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.Threads.sleep;

/**
 * 数据库信息Controller
 * 
 * @author jw
 * @date 2022-06-13
 */
@Controller
@RequestMapping("/system/table")
public class DatabaseTableController extends BaseController
{
    private String prefix = "system/table";

    @Autowired
    private IDatabaseTableService databaseTableService;
    @Autowired
    private IDabaTableService dabaTableService;
    @Autowired
    private IDatabaseDetailsService databaseDetailsService;


    @GetMapping()
    public String table()
    {
        return prefix + "/table";
    }


    @GetMapping("/{dataId}")
    public String devicemanagement(ModelMap modelMap, @PathVariable("dataId") String dataId,DatabaseTable databaseTable) {
        modelMap.put("dataId", dataId);
        try {
            databaseTable.setDataId(Long.valueOf(dataId));
            DabaTable dabaTable = dabaTableService.selectDabaTableByDataId(Long.valueOf(dataId));
            databaseTableService.deleteDatabaseTableByDataId(databaseTable.getTableBelong());
            DatabaseDetails datails = databaseDetailsService.selectDatabaseDetailsByDatabaseId(dabaTable.getDatabaseId());
            List<Map<String,String>> list = MysqlLink.getAllDatabase(datails.getIp(), datails.getPort(), datails.getRoot(), datails.getPassword(), dabaTable.getDataName());
            assert list != null;

            list.forEach(item -> {
                databaseTable.setTableName(item.get("name"));
                databaseTable.setTableRemarks(item.get("remarks"));
                databaseTable.setTableType(item.get("type"));
                databaseTable.setTableBelong(item.get("belong"));
                databaseTableService.insertDatabaseTable(databaseTable);
            });

            sleep(200);
            return prefix + "/table";
        } catch (Exception e) {
            return prefix + "/errortable";
        }
    }
    /**
     * 查询数据库信息列表
     */
    @PostMapping("/list/{dataId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("dataId") String dataId,DatabaseTable databaseTable)
    {
        databaseTable.setDataId(Long.valueOf(dataId));
        startPage();
        List<DatabaseTable> list = databaseTableService.selectDatabaseTableList(databaseTable);
        return getDataTable(list);
    }


    /**
     * 新增数据库信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据库信息
     */
    @Log(title = "数据库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DatabaseTable databaseTable)
    {
        return toAjax(databaseTableService.insertDatabaseTable(databaseTable));
    }

    /**
     * 修改数据库信息
     */
    @GetMapping("/edit/{tableId}")
    public String edit(@PathVariable("tableId") Long tableId, ModelMap mmap)
    {
        DatabaseTable databaseTable = databaseTableService.selectDatabaseTableByTableId(tableId);
        mmap.put("databaseTable", databaseTable);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据库信息
     */
    @Log(title = "数据库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DatabaseTable databaseTable)
    {
        return toAjax(databaseTableService.updateDatabaseTable(databaseTable));
    }

    /**
     * 删除数据库信息
     */
    @Log(title = "数据库信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(databaseTableService.deleteDatabaseTableByTableIds(ids));
    }
}
